﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using DTO;
namespace CungMuaC5.WebUC
{
    public partial class ucDonHang : System.Web.UI.UserControl
    {
        BUS_DonHang donhang = new BUS_DonHang();
        BUS_KhuyenMai khuyenmai = new BUS_KhuyenMai();

        protected void Page_Load(object sender, EventArgs e)
        {
            List<DTO_DonHang> danhsachdonhang = new List<DTO_DonHang>();
            if (Session.IsLogin() == 1) 
                danhsachdonhang = donhang.SelectAll(Session.GetCurrentUser().MaTK.ToString());
            else
                danhsachdonhang.Add(new DTO_DonHang());
            DataList1.DataSource = danhsachdonhang;
            DataList1.DataBind();

            //if (DataList1.Items.Count > 0)
            //{
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    List<DTO_KhuyenMai> sp = new List<DTO_KhuyenMai>();
                    sp = khuyenmai.GetDetails(danhsachdonhang[i].KhuyenMai.ToString());

                    Label tensp = (Label)DataList1.Items[i].FindControl("lblProductName");
                    tensp.Text = sp[0].TenKM;
                    Label dongia = (Label)DataList1.Items[i].FindControl("lblNewPrice");
                    dongia.Text = sp[0].DonGiaMoi + " đ";
                    Label soluong = (Label)DataList1.Items[i].FindControl("lblAmount");
                    Label thanhtien = (Label)DataList1.Items[i].FindControl("lblThanhTien");
                    string temp = null;
                    for (int j = 0; j < dongia.Text.Length; j++)
                        if (dongia.Text[j] >= 48 && dongia.Text[j] <= 59)
                            temp += dongia.Text[j];
                    thanhtien.Text = (Convert.ToInt32(temp) * Convert.ToInt32(soluong.Text)).ToString() + "đ";
                    Label trangthai = (Label)DataList1.Items[i].FindControl("lblTrangThai");
                    switch (trangthai.Text)
                    {
                        case "0":
                            trangthai.Text = "Chưa xử lý";
                            break;
                        case "1":
                            trangthai.Text = "Đã xử lý";
                            break;
                    }
                    Label thanhtoan = (Label)DataList1.Items[i].FindControl("lblThanhToan");
                    switch (thanhtoan.Text)
                    {
                        case "0":
                            thanhtoan.Text = "Chưa thanh toán";
                            break;
                        case "1":
                            thanhtoan.Text = "Đã thanh toán";
                            break;
                    }
                    Label hinhthuc = (Label)DataList1.Items[i].FindControl("lblHinhThuc");
                    switch (hinhthuc.Text)
                    {
                        case "0":
                            if (danhsachdonhang[i].HinhThucNhanHang == 0)
                                hinhthuc.Text = "Thu tiền tại công ty";
                            else
                                hinhthuc.Text = "Thu tiền tại nhà";
                            break;
                        case "1":
                            hinhthuc.Text = "Thẻ ATM nội địa";
                            break;
                        case "2":
                            hinhthuc.Text = "Thẻ tín dụng";
                            break;
                    }
                    Label sum = (Label)DataList1.Items[i].FindControl("lblSum");
                    Label phichuyen = (Label)DataList1.Items[i].FindControl("lblPhiChuyen");
                    temp = null;
                    for (int j = 0; j < phichuyen.Text.Length; j++)
                        if (phichuyen.Text[j] >= 48 && phichuyen.Text[j] <= 59)
                            temp += phichuyen.Text[j];
                    string temp1 = null;
                    for (int j = 0; j < thanhtien.Text.Length; j++)
                        if (thanhtien.Text[j] >= 48 && thanhtien.Text[j] <= 59)
                            temp1 += thanhtien.Text[j];
                    sum.Text = (Convert.ToInt32(temp1) + Convert.ToInt32(temp)).ToString() + " đ";
                }
            //}
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            BUS_DonHang donhang=new BUS_DonHang();
            LinkButton button = (LinkButton)sender;
            DTO_DonHang dh=donhang.SelectDH(button.CommandName);
            int mareceiver = dh.NguoiNhan;
            donhang.Delete(dh.MaDH);
            BUS_NguoiNhan receiver = new BUS_NguoiNhan();
            receiver.Delete(mareceiver);
            Response.Redirect("DanhSachDonHang.aspx");
        }
    }
}